package org.jeecg.modules.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xkcoding.http.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.base.entity.BaseAdInfo;
import org.jeecg.modules.base.entity.LocationHis;
import org.jeecg.modules.base.entity.LocationReal;
import org.jeecg.modules.base.service.IBaseAdInfoService;
import org.jeecg.modules.base.service.ILocationHisService;
import org.jeecg.modules.base.service.ILocationRealService;
import org.jeecg.util.DeleteFlagEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
* @Description: 广告信息
* @Author: jeecg-boot
* @Date:   2020-05-02
* @Version: V1.0
*/
@Slf4j
@Api(tags="广告信息")
@RestController
@RequestMapping("/base/baseAdInfo")
public class BaseAdInfoController extends JeecgController<BaseAdInfo, IBaseAdInfoService> {
   @Autowired
   private IBaseAdInfoService baseAdInfoService;

   @Autowired
   private ILocationHisService locationHisService;

   @Autowired
   private ILocationRealService locationRealService;
   /**
    * 分页列表查询
    *
    * @param baseAdInfo
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "广告信息-分页列表查询")
   @ApiOperation(value="广告信息-分页列表查询", notes="广告信息-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(BaseAdInfo baseAdInfo,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<BaseAdInfo> queryWrapper = QueryGenerator.initQueryWrapper(baseAdInfo, req.getParameterMap());
       Page<BaseAdInfo> page = new Page<BaseAdInfo>(pageNo, pageSize);
       queryWrapper.eq("del_flag", DeleteFlagEnum.NOT_DELETE.getValue());
       IPage<BaseAdInfo> pageList = baseAdInfoService.page(page, queryWrapper);
       return Result.ok(pageList);
   }

   /**
    * 添加
    *
    * @param baseAdInfo
    * @return
    */
   @AutoLog(value = "广告信息-添加")
   @ApiOperation(value="广告信息-添加", notes="广告信息-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody BaseAdInfo baseAdInfo) {
       LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
       baseAdInfo.setCreateTime(new Date());
       baseAdInfo.setCreateBy(sysUser.getUsername());
       baseAdInfo.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
       baseAdInfoService.save(baseAdInfo);
       return Result.ok("添加成功！");
   }

   /**
    * 编辑
    *
    * @param baseAdInfo
    * @return
    */
   @AutoLog(value = "广告信息-编辑")
   @ApiOperation(value="广告信息-编辑", notes="广告信息-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody BaseAdInfo baseAdInfo) {
       LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
       baseAdInfo.setUpdateTime(new Date());
       baseAdInfo.setUpdateBy(sysUser.getUsername());
       baseAdInfoService.updateById(baseAdInfo);
       return Result.ok("编辑成功!");
   }

   /**
    * 通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "广告信息-通过id删除")
   @ApiOperation(value="广告信息-通过id删除", notes="广告信息-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       // baseAdInfoService.removeById(id);
       BaseAdInfo entity = new BaseAdInfo();
       entity.setId(id);
       entity.setDelFlag(DeleteFlagEnum.HAVE_DELETED.getValue());
       baseAdInfoService.updateById(entity);
       return Result.ok("删除成功!");
   }

   /**
    * 批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "广告信息-批量删除")
   @ApiOperation(value="广告信息-批量删除", notes="广告信息-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.baseAdInfoService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功！");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "广告信息-通过id查询")
   @ApiOperation(value="广告信息-通过id查询", notes="广告信息-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       BaseAdInfo baseAdInfo = baseAdInfoService.getById(id);
       return Result.ok(baseAdInfo);
   }


    /**
     * 采编app广告页
     *
     * @param baseAdInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "广告信息-采编app广告页")
    @ApiOperation(value="广告信息-采编app广告页", notes="广告信息-采编app广告页")
    @PostMapping(value = "/editAppAds")
    public Result<?> editAppAds(BaseAdInfo baseAdInfo,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   @RequestParam(name="device_no", required = false,defaultValue = "") String device_no,
                                   @RequestParam(name="longitude", required = false,defaultValue = "") String longitude,
                                   @RequestParam(name="latitude", required = false,defaultValue = "") String latitude,
                                   HttpServletRequest req) {
        QueryWrapper<BaseAdInfo> queryWrapper = QueryGenerator.initQueryWrapper(baseAdInfo, req.getParameterMap());
        Page<BaseAdInfo> page = new Page<BaseAdInfo>(pageNo, pageSize);
        queryWrapper.eq("del_flag", DeleteFlagEnum.NOT_DELETE.getValue());
        queryWrapper.eq("ad_position", 1);

        queryWrapper.lt("begin_date",new Date());
        queryWrapper.gt("end_date",new Date());

        queryWrapper.orderByDesc("sort");
        IPage<BaseAdInfo> pageList = baseAdInfoService.page(page, queryWrapper);
        //

        if (!StringUtil.isEmpty(longitude) && !StringUtil.isEmpty(latitude)){
            updateLocation(req.getRemoteAddr(),device_no,longitude,latitude);
        }
        return Result.ok(pageList);
    }


    public void updateLocation(String device_addr,String device_no,String longitude,String latitude){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LocationHis locationHis=new LocationHis();
        LocationReal locationReal=new LocationReal();
        locationHis.setUserId(sysUser.getId());
        if (!StringUtil.isEmpty(sysUser.getAvatar())){
            locationHis.setUserHead(sysUser.getAvatar());
            //
            locationReal.setUserHead(sysUser.getAvatar());
        }
        if (!StringUtil.isEmpty(sysUser.getUsername())){
            locationHis.setUserName(sysUser.getUsername());
            //
            locationReal.setUserName(sysUser.getUsername());
        }
        locationHis.setDeviceAddr(device_addr);
        locationHis.setDeviceNo(device_no);
        locationHis.setLatitude(latitude);
        locationHis.setLongitude(longitude);
        locationHisService.save(locationHis);

        //删除之前的数据
        locationRealService.remove(new QueryWrapper<LocationReal>().eq("user_id",sysUser.getId()));

        locationReal.setUserId(sysUser.getId());
        locationReal.setDeviceAddr(device_addr);
        locationReal.setDeviceNo(device_no);
        locationReal.setLatitude(latitude);
        locationReal.setLongitude(longitude);
        locationRealService.save(locationReal);
    }


}
